package org.adorsys.aderp.aderplogin.client.oauth;

import org.adorsys.aderp.aderplogin.client.common.Oauth2User;
import org.adorsys.aderp.aderplogin.client.common.TokenProvider;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

/**
 * Instantiates and hold a reference to the system user.
 * 
 * @author francis
 *
 */
public class Oauth2SystemUserService implements InitializingBean, TokenProvider {

	private Oauth2User systemUser;

	public Oauth2User getSystemUser() {
		return systemUser;
	}
	
	public Oauth2User getSystemUser(String tokenName) {
		if(!StringUtils.equals(tokenName, systemUser.getOauthToken()))
			return null;
		
		return systemUser;
	}

	public void setSystemUser(Oauth2User systemUser) {
		this.systemUser = systemUser;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		Assert.notNull(systemUser, "Property SystemUser must be set");
	}

	@Override
	public String getToken() {
		return systemUser.getOauthToken();
	}
}
